﻿using Microsoft.Extensions.DependencyInjection;
using Quartz.Spi;
using Quartz;

public class DefaultScheduleServiceFactory : IJobFactory
{
    private readonly IServiceProvider serviceProvider;

    public DefaultScheduleServiceFactory(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        //返回jobType对应类型的实例
        //这里直接获取IJob接口服务，但要在IoC容器中注册成IJob形式，也就是说这里的注入和注册要对应
        var o = serviceProvider.GetService<IJob>();
        return o;
    }

    public void ReturnJob(IJob job)
    {
        var disposable = job as IDisposable;

        disposable?.Dispose();
    }
}